﻿using Core;
using EnvDTE;

namespace RevisionItNow.Features
{
	public class HistoryFeature : BindableCommandFeature
	{
		public HistoryFeature(string name, string command, Ide ide, OutputPane outputPane)
		: base(name, command, ide, outputPane)
		{
		}

		public override bool IsAvailable
		{
			get { return FindSelectedItemPath() != null; }
		}

		public override void OnExecute()
		{
            Ide.ActivateOutputWindow();
		    OutputPane.JumpToBottom();
            Mercurial.History(FindSelectedItemPath());
		}

		private string FindSelectedItemPath()
		{
			foreach (SelectedItem item in Ide.Dte.SelectedItems)
			{
				if (item.Project != null)
					return item.Project.FullName;

				if (item.ProjectItem != null)
					return item.ProjectItem.FileNames[0];
			}

			return null;
		}
	}
}